﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extjs_ReGSharp.Sorting;
using System.Collections.Specialized;
using Extjs_ReGSharp.InputParsing;
using Extjs_ReGSharp.Interfaces;
using System.Collections;

namespace Extjs_ReGSharp
{
	public class GridSource<T> where T : IGridDisplayItem
	{
		#region Data Memebers

		private List<T> m_SourceItems;

		#endregion

		#region Constructor

		public GridSource(List<T> sourceItems)
		{
			m_SourceItems = sourceItems;
		}

		#endregion

		#region Properties

		public List<T> SourceItems
		{
			get
			{
				return m_SourceItems;
			}
		}

		#endregion

		#region Internal Methods

		internal GridResult<T> GetItems(Filter<T> filter, Sorter<T> sorter, PageData<T> page)
		{
			List<T> currentView = filter == null ?
				m_SourceItems :
				m_SourceItems.Where(item => filter.MainCriteria.IsValid(item)).ToList();

			if (sorter != null && sorter.HasSortingBehaviour)
			{
				currentView.Sort(sorter.Compare);
			}

			GridResult<T> result = new GridResult<T>(currentView.Where(page.IsInPage).ToList(), currentView.Count);

			return result;
		}

		#endregion
	}
}
